function [fidSignal, fidSpectrum] = GET_FIDResonance(obj, varargin)
%GET_FIDRESONANCE Summary of this function goes here
%   Detailed explanation goes here
    L=log4m.getLogger();
    p=inputParser;
    p.addParameter('isotope', 'both', @(x) any(validatestring(x,{'129', '131', 'both'})) );
    p.addParameter('threshold', 10.0, @(x) isscalar(x) && x > 1.0);
    p.parse(varargin{:});

    fidSignal = obj.GET_FIDSignal('isotope', p.Results.isotope);
    fidSpectrum =obj.GET_FIDSpectrum(fidSignal,'isotope', p.Results.isotope);
    
    switch p.Results.isotope
        case 'both'
            if fidSpectrum{1}.resonance.frequency > fidSpectrum{2}.resonance.frequency
                res129 = fidSpectrum{1}.resonance;
                res131 = fidSpectrum{2}.resonance;
            else
                res129 = fidSpectrum{2}.resonance;
                res131 = fidSpectrum{1}.resonance;
            end            
            f129est = obj.getXe129EstFreq();
            f131est = obj.getXe131EstFreq();
            delta129 = res129.frequency - f129est;
            delta131 = res131.frequency - f131est;
            
            if abs(delta129) > p.Results.threshold || abs(delta131) > p.Results.threshold
                L.info('XeNMRSystem::GET_FIDResonance', ...
                       sprintf('Unreasonable NMR frequencies (f129=%8.6f, f131=%8.6f). Driving frequencies (f129=%8.6f, f131=%8.6f) are not changed.', ...
                                res129.frequency, res131.frequency, obj.resonance129.frequency, obj.resonance131.frequency));
            else
                obj.resonance129 = res129;
                obj.resonance131 = res131;
                obj.go2Resonance();
                L.info('XeNMRSystem::GET_FIDResonance', ...
                       sprintf('f129=%8.4f Hz, gamma129=%3.2f mHz, gof129=%3.4f; f131=%8.4f Hz, gamma131=%3.2f mHz, gof131=%3.4f; ratio=%8.7f', ...
                               obj.resonance129.fit.w0, 1000*obj.resonance129.fit.gamma, obj.resonance129.gof.rsquare, ...
                               obj.resonance131.fit.w0, 1000*obj.resonance131.fit.gamma, obj.resonance131.gof.rsquare, ...
                               obj.getFrequencyRatio));
            end
        case '131'
            res131 = fidSpectrum{1}.resonance;
            f131est = obj.getXe131EstFreq();
            delta131 = res131.frequency - f131est;
            
            if abs(delta131) > 20.0
                L.info('XeNMRSystem::GET_FIDResonance', ...
                       sprintf('Unreasonable NMR frequencies (f131=%8.6f). Driving frequencies (f131=%8.6f) is not changed.', ...
                                res131.frequency, obj.resonance131.frequency));

            else
                obj.resonance131 = res131;
                obj.go2Resonance131();
            end
        case '129'
            res129 = fidSpectrum{1}.resonance;
            f129est = obj.getXe129EstFreq();
            delta129 = res129.frequency - f129est;
            
            if abs(delta129) > 20.0
                L.info('XeNMRSystem::GET_FIDResonance', ...
                       sprintf('Unreasonable NMR frequencies (f129=%8.6f). Driving frequencies (f129=%8.6f) is not changed.', ...
                                res129.frequency, obj.resonance129.frequency));
            else
                obj.resonance129 = res129;
                obj.go2Resonance129();
            end
    end
end

